package cn.fbs.controller;

import cn.fbs.model.TCodeCategoryType;
import cn.fbs.param.Params;
import cn.fbs.service.CategoryService;
import cn.fbs.vo.CateAndGroupVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api(tags ="标签模块")
@RestController
@RequestMapping("category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @ApiOperation("查询所有的标签")
    @GetMapping("findAll")
    public List<TCodeCategoryType> findAll() {
        return categoryService.findAll();
    }

    @ApiOperation("查询所有的标签-按照标签首字母分组")
    @GetMapping("findAllGroupByCateGory")
    public Map<String, List<TCodeCategoryType>> findAllGroupByCateGory() {
        return categoryService.findAllGroupByCateGory();
    }

    @ApiOperation("查询相同groupId标签")
    @ApiImplicitParam(name = "groupId", value = "标签组外键")
    @GetMapping("findByGroupIdQuery")
    public List<TCodeCategoryType> findByGroupIdQuery(String groupId) {
        return categoryService.findByGroupIdQuery(groupId);
    }

    @ApiOperation("新增标签")
    @PostMapping("add")
    public String insert(@RequestBody @ApiParam("标签实体") TCodeCategoryType tCodeCategoryType) {
        String categoryName = tCodeCategoryType.getCategoryName();
        if (StringUtils.isBlank(categoryName)){
            return "500,标签名称不可以为空";
        }
        tCodeCategoryType.setCategoryName(categoryName.trim());
        String info = categoryService.insert(tCodeCategoryType);

        return info;
    }

    @ApiOperation("更新标签")
    @PostMapping("update")
    public String update(@RequestBody @ApiParam("标签实体") TCodeCategoryType tCodeCategoryType) {
        String categoryName = tCodeCategoryType.getCategoryName();
        if (StringUtils.isBlank(categoryName)){
            return "500,标签名称不可以为空";
        }
        tCodeCategoryType.setCategoryName(categoryName.trim());
        String info =  categoryService.update(tCodeCategoryType);
        return info;
    }

    @ApiOperation("根据id删除标签")
    @ApiImplicitParam(name = "id", value = "标签id")
    @PostMapping("delete")
    public String delete(@RequestBody
                       @ApiParam(name = "id", value = "主键") Params params) {
        return categoryService.delete(params.getId());
    }

    @ApiOperation("根据id查询标签")
    @ApiImplicitParam(name = "id", value = "标签id")
    @GetMapping("get")
    public TCodeCategoryType get(Integer id) {
        return categoryService.selectByPrimaryKey(id);
    }

    @ApiOperation("根据标签名词模糊查询标签")
    @ApiImplicitParam(name = "categoryName", value = "标签名称")
    @GetMapping("search")
    public List<TCodeCategoryType> likeQuery(String categoryName) {
        return categoryService.likeQuery(categoryName);
    }

    @ApiOperation("查询标签组及其下标签--标签组下不为空的数据")
    @GetMapping("categoryAndGroup")
    public List<CateAndGroupVo> categoryAndGroup() {
        return categoryService.categoryAndGroup();
    }

    @ApiOperation("查询标签组及其下标签--所有的数据")
    @GetMapping("allCategoryAndGroup")
    public List<CateAndGroupVo> allCategoryAndGroup() {
        return categoryService.allCategoryAndGroup();
    }
    @ApiOperation("查询固有标签组及其下标签")
    @GetMapping("inherentLabel")
    public List<CateAndGroupVo> inherentLabel() {
        return categoryService.inherentLabel();
    }
}
